/*
 * BlockingQueue.cpp
 * Queue that sleeps calling thread when invoking take and waks up when
 * queue is not empty. It's to implement Producer-Consumer Pattern in
 *  Created on: 20/06/2011
 *      Author: wadi
 */

#include "blockingqueue.h"

BlockingQueue::BlockingQueue()
{
  closed=false;
}

BlockingQueue::~BlockingQueue()
{
}

void BlockingQueue::closeQueue()
{
  Lock l(mutex);
  closed=true;
  if(msgQueue.empty())
    {
      cv.notify();
    }
}
void BlockingQueue::push(std::vector<char> m){
  Lock l(mutex);
  bool const estabaVacia=msgQueue.empty();
  msgQueue.push(m);
  if(estabaVacia)
    {
      cv.notify();
    }
}
std::vector<char> BlockingQueue::take(){
  Lock lock(mutex);
  if(msgQueue.empty())
    {
      cv.wait(mutex);
    }
  if(!closed){
      std::vector<char> retorno=msgQueue.front();
      msgQueue.pop();
      return retorno;
  }

  return std::vector<char>();

}
